package com.jvtd.nettyim.im.client.handler;

import com.jvtd.nettyim.im.protocol.request.LoginRequestPacket;
import com.jvtd.nettyim.im.protocol.response.LoginResponsePacket;
import com.jvtd.nettyim.im.session.Session;
import com.jvtd.nettyim.im.util.SessionUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.UUID;

/**
 * @author Created by neal.zhang
 * @date 2020/5/29 - 18:25
 */
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponse) throws Exception {
        String userId = loginResponse.getUserId();
        String userName = loginResponse.getUserName();
        if (loginResponse.isSuccess()) {
            System.err.println("登陆成功 :"+userName+", userId :"+userId);
            //保存登录状态
            SessionUtil.bindSession(new Session(userId,userName),ctx.channel());
        }else {
            System.err.println("登陆失败-->"+userName+", reason:"+loginResponse.getReason());
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.err.println("连接关闭.");
    }
}
